package cn.emailChat.ai.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @Description
 * @Author susu
 * @Date 2025/8/18
 */

@Configuration
public class MailSenderConfig {

    @Bean
    public JavaMailSender javaMailSender(
            @Value("${spring.mail.host}") String host,
            @Value("${spring.mail.port}") int port,
            @Value("${spring.mail.username}") String username,
            @Value("${spring.mail.password}") String password,
            @Value("${spring.mail.properties.mail.smtp.auth:true}") boolean auth,
            @Value("${spring.mail.properties.mail.smtp.ssl.enable:true}") boolean sslEnable,
            @Value("${spring.mail.properties.mail.smtp.timeout:10000}") int timeout
    ) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host);
        sender.setPort(port);
        sender.setUsername(username);
        sender.setPassword(password);

        Properties p = sender.getJavaMailProperties();
        p.put("mail.smtp.auth", String.valueOf(auth));
        p.put("mail.smtp.ssl.enable", String.valueOf(sslEnable));
        p.put("mail.smtp.timeout", String.valueOf(timeout));
        // 如走 587:
        // p.put("mail.smtp.starttls.enable", "true");
        return sender;
    }
}
